<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;
use app\common\model\Pro;

class DistrictPro extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\DistrictPro';

    protected $pro =   '\app\common\model\Pro';

    //关联模型
    protected $hasOne = ['pro', 'district'];

    public function index($id)
    {
        //print('<pre>');print_r($this->pro::proSelectTable());die;
        return $this->setTable($id);
    }

    //自动构建table,按需修改
    public function setTable($id)
    {


        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '标题', 'width' => 120, 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow([
                'field' => 'pro_id', 'title' => '所属项目场所', 'width' => 120, 'format' => 'select',
                'value' => $this->pro::proSelectTable(),
                'modify' => 'edit',
                'templet' => '<div>{{d.pro.title}}</div>'
            ])
            ->addRow(['field' => 'districttitle', 'title' => '所属区域', 'templet' => '<div>{{d.district.title}}</div>'])
            ->addRow(['field' => 'sort', 'title' => '排序', 'width' => 120, 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'basenum', 'title' => '底数', 'width' => 120, 'modify' => 'add|edit', 'format' => 'number'])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            ->addRow(['field' => 'update_time', 'title' => '更新时间'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000], 'delete'])
            ->addTopButtons([
                'add' => [
                    'type' => 'add',
                    'title' => '新增',
                    'width' => 1000,
                    'height' => 1000,
                    'href' => url('districtpro/add', ['name' => request()->controller() . '_' . request()->action(), 'id' => $id])->build(),
                ], 'deleteAll'
            ])

            ->addExtraMap(['district_id' => $id])
            ->setSearch($this->setSearch())
            ->fetch('districtpro/index');
    }
    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance();
    }

    public function add($name = '', $tpl = 'districtpro/add')
    {
        $params = $this->request->param();
        //print_r($this->pro::proSelect());die;
        View::assign('district_id', $params['id']);
        View::assign('prolist', $this->pro::where(['is_del'=>0])->order('sort asc')->field('id,title')->select()->toArray());
        View::assign('sort', ($this->model::where(array('district_id' => $params['id'], 'is_del' => 0))->max('sort') + 1));
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }
}
